package com.frxuexi.park2210.repair

import android.content.DialogInterface
import android.content.DialogInterface.OnClickListener
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.RepairBean
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.park2210.R
import com.frxuexi.park2210.adapter.RepairAdapter
import com.frxuexi.park2210.databinding.ActivityRepairBinding
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/RepairActivity")
@AndroidEntryPoint
class RepairActivity : BaseActivity<ActivityRepairBinding,RepairViewModel>(){
    val repairAdapter by lazy { RepairAdapter() }
    override fun initData() {

        vm.allRepairSuc.observe(this){
            val repairBean = Gson().fromJson(it, RepairBean::class.java)
                .filter {
                    it.repairData!="string" && !it.repairData.isNullOrEmpty() && !it.repairDescribe.isNullOrEmpty()
                }.sortedByDescending { it.repairId }
            repairAdapter.addData(repairBean)
            repairAdapter.notifyDataSetChanged()
        }

        /*vm.selAppointSuc.observe(this){
            Log.i("===",it)
        }*/
        lifecycleScope.launch {
            vm.selAppointSuc.collect{
                if(it.isNotEmpty()){
                    ToastUtils.showLong(it)
                    Log.i("===",it)
                }
            }
        }
    }

    override fun initView() {
        binding.headerRepair.apply {
            headerBack.setOnClickListener {
                finish()
            }
            headerTitle.setText("维修管理")
            change.visibility=View.VISIBLE
            change.text="管理"
            change.setOnClickListener {
                if(change.text=="管理"){
                    change.text="完成"
                    binding.cbAll.visibility=View.VISIBLE
                    /*repairAdapter.data.forEach {
                        it.isShow=true
                    }*/
                    for (i in 0..repairAdapter.data.size-1){
                        repairAdapter.data.get(i).isShow=true
                        repairAdapter.notifyItemChanged(i)
                    }

                }else{//完成点击事件
                    change.text="管理"
                    binding.cbAll.visibility=View.GONE
                    for (i in 0..repairAdapter.data.size-1){
                        val it = repairAdapter.data.get(i)
                        it.isShow=false
                        if(it.isChecked){
                            vm.selAppoint(
                                mapOf(
                                    "repairId" to it.repairId, "repairUserId" to 455,
                                    "repairTime" to vm.getDateTime(System.currentTimeMillis())
                                )
                            )
                        }
                        repairAdapter.notifyItemChanged(i)
                    }

                }

            }
        }
        binding.rvRepair.apply {
            adapter=repairAdapter
            layoutManager=LinearLayoutManager(this@RepairActivity)
        }

        repairAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/RepairDetailActivity")
                .withSerializable("repair",repairAdapter.data.get(position)).navigation()
        }

        binding.cbAll.apply {
            setOnClickListener {
                repairAdapter.data.forEach {
                    it.isChecked=isChecked
                }
                repairAdapter.notifyDataSetChanged()
            }
        }

        repairAdapter.addChildClickViewIds(R.id.btn_approve,R.id.repair_cb)
        repairAdapter.setOnItemChildClickListener { adapter, view, position ->
            val beanItem = repairAdapter.data.get(position)
            if(view.id==R.id.btn_approve) {
                val repairUserId = beanItem.repairUserId
                if (repairUserId == 0) {
                    val lists = arrayOf("王师傅", "李师傅", "赵师傅")
                    AlertDialog.Builder(this)
                        .setTitle("指定维修人")
                        .setSingleChoiceItems(lists, 0, object : OnClickListener {
                            override fun onClick(dialog: DialogInterface?, which: Int) {
                                vm.selAppoint(
                                    mapOf(
                                        "repairId" to beanItem.repairId, "repairUserId" to 455,
                                        "repairTime" to vm.getDateTime(System.currentTimeMillis())
                                    )
                                )
                                ToastUtils.showLong("已指定${lists[which]}")
                                dialog?.dismiss()

                                beanItem.repairUserId = 455
                                beanItem.repairTime = vm.getDateTime(System.currentTimeMillis())
                                repairAdapter.notifyItemChanged(position)

                            }
                        }).create()
                        .show()
                }
            }else{
                beanItem.isChecked=!beanItem.isChecked
                if(repairAdapter.data.any { it.isChecked==false }){
                    binding.cbAll.isChecked=false
                }else{
                    binding.cbAll.isChecked=true
                }
            }
        }
        vm.getAllRepairs()
    }

}